{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4acca4af-be7f-47fa-9597-b77841d1c775",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4f837fac-9bcf-4a43-a875-1bf3b6f6d649",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = \"\"\"\\\n",
    "姓名,年龄,成绩\n",
    "张三,18,85\n",
    "李四,20,72\n",
    "王五,19,90\n",
    "孙七,21,78\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ecdb478-e2b2-4dca-b5fa-9ef1556b85f4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c9d6c469-8dc6-4bcd-86bc-24fbcb60b954",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['姓名,年龄,成绩', '张三,18,85', '李四,20,72', '王五,19,90', '孙七,21,78']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.split('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fe6d11dd-b9f6-45b8-aa7d-7ef1ad1dc7ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['姓名', '年龄', '成绩']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lines = data.split('\\n')\n",
    "lines[0].split(',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "016c0c41-c68f-4210-81d7-f4a9fce9c79f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2b2971fe-52ad-439b-a152-35e44f5fba8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['姓名', '年龄', '成绩'],\n",
       " ['张三', '18', '85'],\n",
       " ['李四', '20', '72'],\n",
       " ['王五', '19', '90'],\n",
       " ['孙七', '21', '78']]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rows = []\n",
    "for ln in data.split('\\n'):\n",
    "    cols = ln.split(',')\n",
    "    rows.append(cols)\n",
    "\n",
    "rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9ad6bcdb-c9c9-4288-b189-51b8b6ea7121",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['姓名', '年龄', '成绩']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colnames = rows[0]\n",
    "colnames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9d3ccd06-00c3-4072-b397-ac24728e5af8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'姓名': '李四', '年龄': '20', '成绩': '72'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{k:v for k,v in zip(colnames, rows[2])}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1433199c-25b4-44c1-96e2-289374b65273",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'姓名': '张三', '年龄': '18', '成绩': '85'},\n",
       " {'姓名': '李四', '年龄': '20', '成绩': '72'},\n",
       " {'姓名': '王五', '年龄': '19', '成绩': '90'},\n",
       " {'姓名': '孙七', '年龄': '21', '成绩': '78'}]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students = []\n",
    "for student in rows[1:]:\n",
    "    student = {k:v for k,v in zip(colnames, student)}\n",
    "    students.append(student)\n",
    "students"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "43d40aff-3b31-4cd0-bdd3-9e2aa80a9f26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '18', '85']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "student =rows[1:][0]\n",
    "student"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "edc39242-9f41-459e-bb1b-b7356122c007",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('姓名', '张三'), ('年龄', '18'), ('成绩', '85')]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[t for t in zip(colnames, student)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9e299597-15b0-4267-bbe1-c2270aa8eec6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'姓名': '张三', '年龄': '18', '成绩': '85'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{k:v for k,v in zip(colnames, student)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9b6211f-1339-4b46-af2e-c55c243e4a52",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d691902e-7714-48f5-8537-ea4684a087d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'姓名': '张三', '年龄': '18', '成绩': '85'},\n",
       " {'姓名': '李四', '年龄': '20', '成绩': '72'},\n",
       " {'姓名': '王五', '年龄': '19', '成绩': '90'},\n",
       " {'姓名': '孙七', '年龄': '21', '成绩': '78'}]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lines = data.split('\\n')\n",
    "colnames = lines[0].split(',')\n",
    "students = [dict(zip(colnames, line.split(','))) for line in lines[1:]]\n",
    "students"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "15651292-7ab9-4ee7-845f-b069fb91dc64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'姓名': '张三', '年龄': 18, '成绩': 85.0},\n",
       " {'姓名': '李四', '年龄': 20, '成绩': 72.0},\n",
       " {'姓名': '王五', '年龄': 19, '成绩': 90.0},\n",
       " {'姓名': '孙七', '年龄': 21, '成绩': 78.0}]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for r in students:\n",
    "    r['年龄'] = int(r['年龄'])\n",
    "    r['成绩'] = float(r['成绩'])\n",
    "\n",
    "students"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "405e3d29-ff04-4467-9d25-9cd6ea280cdc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4ae96a29-0986-4fb6-bc07-a273401eca98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'张三': 85.0, '李四': 72.0, '王五': 90.0, '孙七': 78.0}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{s['姓名']:s['成绩'] for s in students if s['成绩'] >= 60}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
